package GEF;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

import IU.Janela;


public class Pedido {
	
	SERVER_ADDR sa;
	Socket socket;
	
	ObjectInputStream in;
	ObjectOutputStream out;


	private boolean ATUALIZA_BD = true;
	
	public Pedido () {
		try {
			sa = new SERVER_ADDR();
		} catch (ClassNotFoundException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Janela.ERRORMSG=e.toString();
		}
	}
	
	public boolean Conectar (){
		
		try {
			socket = new Socket(sa.getIP(),sa.getPORTO());
		
			 out=new ObjectOutputStream(socket.getOutputStream());
			 in =new ObjectInputStream(socket.getInputStream());
		} catch (IOException ex) {
			Logger.getLogger(Pedido.class.getName()).log(Level.SEVERE, null, ex);
			Janela.ERRORMSG=ex.toString();
			return false;
		}
		return true;
	}
	
	public void Desconectar () throws ClassNotFoundException, IOException{
		
		String buf = "DESCONECTAR";
		try{
			out.writeObject(buf);
		}
		catch (IOException ex){
			Logger.getLogger(Pedido.class.getName()).log(Level.SEVERE, null, ex);
			Janela.ERRORMSG=ex.toString();
		}
		
		try {
			in.close();
			out.close();
            socket.close();
		} catch (IOException ex) {
			Logger.getLogger(Pedido.class.getName()).log(Level.SEVERE, null, ex);
			Janela.ERRORMSG=ex.toString();
		}
	}
	
	public boolean Login (String username, char[] cs){
		String buf = "LOGIN "+username+" "+cs.toString();
		boolean obj=false;
		try{
			out.writeObject(buf);
			
			obj = (boolean) in.readObject();
		}
		catch (IOException ex){
			Logger.getLogger(Pedido.class.getName()).log(Level.SEVERE, null, ex);
			Janela.ERRORMSG=ex.toString();
			return false;
		}
		catch (ClassNotFoundException ex) {
            Logger.getLogger(Pedido.class.getName()).log(Level.SEVERE, null, ex);
            Janela.ERRORMSG=ex.toString();
            return false;
		}
		return obj;
		
	}

//	class Thread_Listener extends Thread {
//		Socket socket;
//		ObjectInputStream in;
//		// Cliente c;
//		boolean close;
//
//		Thread_Listener(Socket s) throws IOException {
//			socket = s;
//			in = new ObjectInputStream(socket.getInputStream());
//			// this.c=c;
//			close = false;
//		}
//
//		@Override
//		public void run() {
//			
//			while(!close){
//	            try {
//	                
//	                Object obj =null;
//	           
//	                obj = in.readObject();
//	            }
//	            catch (IOException ex) {
//	                Logger.getLogger(Thread_Listener.class.getName()).log(Level.SEVERE, null, ex);
//	            } catch (ClassNotFoundException ex) {
//	                Logger.getLogger(Thread_Listener.class.getName()).log(Level.SEVERE, null, ex);
//	            }
//		}
//	}

//	class Thread_Writer extends Thread {
//		
//		
//	    ObjectOutputStream out;
//	    //Cliente c;
//	    boolean close;
//
//	    public Thread_Writer(Socket socket) throws IOException {
//	        socket = socket;
//	        out = new ObjectOutputStream(socket.getOutputStream());
//	        this.c = c;
//	        close = false;
//	    }
//
//	    @Override
//	    public void run() {
//
//	        while (!close) {
//
//	            try {
//	                Scanner scan = new Scanner(System.in);
//	                String sc = scan.nextLine();
//
//	                out.writeObject(sc);
//
//	                if (sc.equalsIgnoreCase("Sair")) {
//	                    close = true;
//	                }
//
//	            } catch (IOException ex) {
//	                Logger.getLogger(Thread_Writer.class.getName()).log(Level.SEVERE, null, ex);
//	            }
//
//	        }
//	    }
//	
//	}

	public boolean isATUALIZA_BD() {
		return ATUALIZA_BD;
	}

	public void setATUALIZA_BD(boolean aTUALIZA_BD) {
		ATUALIZA_BD = aTUALIZA_BD;
	}
}
